<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px ; width:100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="add(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="updata(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="del(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      title="新增部门"
      :visible.sync="dialogVisible"
      width="80%"
      @close="close"
      @open="getmanagerList"
    >
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width:80%" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:80%" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
            <el-option
              v-for="item in managerlist"
              :key="item.id"
              :value="item.username"
              :label="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:80%" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addForm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { fetchDepartmentList, getManager, addFormInfo, getdataInfo, modyInfo, delInfo } from '@/api/department'
import { totree } from '@/utils/transTree'
export default {
  name: 'DepartMent',
  data() {
    return {
      list: [{
        name: '财务部',
        children: [
          {
            name: '财务核算部'
          },
          {
            name: '税务核算部'
          }
        ]
      }],
      defaultProps: {
        label: 'name'
      },
      dialogVisible: false,
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      },
      managerlist: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await fetchDepartmentList()
      this.list = totree(res.depts)
    },
    add(id) {
      this.dialogVisible = true
      this.form.pid = id
    },
    setdataInfo() {
      this.form = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        id: ''
      }
    },
    async del(id) {
      await delInfo(id)
      this.$message.success('删除部门成功')
      this.getList()
    },
    close() {
      this.$refs.form.resetFields()
    },
    async getmanagerList() {
      const res = await getManager()
      this.managerlist = res
    },
    async updata(id) {
      this.dialogVisible = true
      const res = await getdataInfo(id)
      this.form = res
    },
    addForm() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.form.id) {
            await modyInfo(this.form)
          } else {
            await addFormInfo(this.form)
          }
          this.$message.success(`${this.form.id ? '修改' : '添加'}部门成功`)
          this.dialogVisible = false
          this.getList()

          this.setdataInfo()
        } else {
          return false
        }
      })
    }
  }
}
</script>
